package com.douban.business;

import java.io.File;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import junit.framework.TestCase;


import org.apache.xerces.xni.parser.XMLInputSource;
import org.apache.xerces.xni.parser.XMLParserConfiguration;
import org.cyberneko.html.HTMLConfiguration;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;

import com.douban.business.vo.ItemVO;
import com.douban.constants.DoubanConstant;

public class DoubanItemListHandlerTest extends TestCase {
	
	DoubanItemListHandler unitundertest= null;
	private String userDir = System.getProperty("user.dir");

	public void testParser() throws Exception{
		
		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "getSimpleItem.xml";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, null, null, fr, DoubanConstant.CHAR_SET.getValue() );	
		
		
//		XMLInputSource src = new XMLInputSource(null,"C:\\OtherDevelopments\\EclipseWorkspace\\TempApplication\\test\\getSimpleItem.xml",null);
//		src.setEncoding(DoubanConstant.CHAR_SET.getValue());
//		
		xmlReaderParser.parse(src);
		assertEquals(true, unitundertest.getMap()!=null);
		
	}
	
	public void testFullPageParser() throws Exception{

		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "FullPageParsingSample.xml";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, fileName, null, fr, DoubanConstant.CHAR_SET.getValue() );	
			
		xmlReaderParser.parse(src);
		printOutItemsMap(unitundertest.getMap());
		assertEquals(true, unitundertest.getMap().size()==30);	
	}
	
	public void testParsingBookDo() throws Exception {
		
		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "bookDo.txt";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, fileName, null, fr, DoubanConstant.CHAR_SET.getValue() );	
			
		xmlReaderParser.parse(src);
		printOutItemsMap(unitundertest.getMap());
		assertEquals(true, unitundertest.getMap().size()==10);				
	}
	
	public void testParsingBookWish() throws Exception {
		
		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "bookWish.txt";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, fileName, null, fr, DoubanConstant.CHAR_SET.getValue() );	
			
		xmlReaderParser.parse(src);
		printOutItemsMap(unitundertest.getMap());
		assertEquals(true, unitundertest.getMap().size()==30);				
	}
	
	public void testParsingBookCollect() throws Exception {
		
		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "bookCollect.txt";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, fileName, null, fr, DoubanConstant.CHAR_SET.getValue() );	
			
		xmlReaderParser.parse(src);
		printOutItemsMap(unitundertest.getMap());
		assertEquals(true, unitundertest.getMap().size()==23);				
	}
	
	public void testParsingMovieDo() throws Exception {
		
		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "movieDo.txt";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, fileName, null, fr, DoubanConstant.CHAR_SET.getValue() );	
			
		xmlReaderParser.parse(src);
		printOutItemsMap(unitundertest.getMap());
		assertEquals(true, unitundertest.getMap().size()==9);				
	}

	public void testParsingMovieWish() throws Exception {
		
		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "movieWish.txt";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, fileName, null, fr, DoubanConstant.CHAR_SET.getValue() );	
			
		xmlReaderParser.parse(src);
		printOutItemsMap(unitundertest.getMap());
		assertEquals(true, unitundertest.getMap().size()==30);				
	}
	
	public void testParsingMovieCollect() throws Exception {
		
		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "movieCollect.txt";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, fileName, null, fr, DoubanConstant.CHAR_SET.getValue() );	
			
		xmlReaderParser.parse(src);
		printOutItemsMap(unitundertest.getMap());
		assertEquals(true, unitundertest.getMap().size()==30);				
	}
	
	
	public void testParsingMusicDo() throws Exception {
		
		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "musicDo.txt";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, fileName, null, fr, DoubanConstant.CHAR_SET.getValue() );	
			
		xmlReaderParser.parse(src);
		printOutItemsMap(unitundertest.getMap());
		assertEquals(true, unitundertest.getMap().size()==13);				
	}

	public void testParsingMusicWish() throws Exception {
		
		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "musicWish.txt";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, fileName, null, fr, DoubanConstant.CHAR_SET.getValue() );	
			
		xmlReaderParser.parse(src);
		printOutItemsMap(unitundertest.getMap());
		assertEquals(true, unitundertest.getMap().size()==30);				
	}
	
	public void testParsingMusicCollect() throws Exception {
		
		unitundertest = new DoubanItemListHandler();
		XMLParserConfiguration xmlReaderParser = new HTMLConfiguration();
		xmlReaderParser.setDocumentHandler(unitundertest);

		String fileName = userDir + File.separator + "test" + File.separator+ "musicCollect.txt";
		FileReader fr = new FileReader(fileName);
		
		XMLInputSource src = new XMLInputSource(null, fileName, null, fr, DoubanConstant.CHAR_SET.getValue() );	
			
		xmlReaderParser.parse(src);
		printOutItemsMap(unitundertest.getMap());
		assertEquals(true, unitundertest.getMap().size()==30);				
	}
	
	private void printOutItemsMap(Map theMap){
		List keyList = new ArrayList(theMap.keySet()); 
		Collections.sort(keyList); 
		Iterator keysItr= keyList.iterator();
		while (keysItr.hasNext()) {
			String key = (String) keysItr.next();
			ItemVO itemvo = (ItemVO)theMap.get(key);
			System.out.println(itemvo.getId()+" "+itemvo.getTitle()+" "+itemvo.getIntro()+" "+itemvo.getDate());
			
		}
	}
}
